package com.meizitu;

import com.meizitu.entity.Image;
import com.meizitu.entity.LinkSet;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则表达式匹配 图片路径
 *
 * @author zhouinfo
 * @Create Date 2016-07-31 19:54
 */
public class PatternMatcher {

    // 获取img标签正则
    public static final String IMGURL_REG = "<img.*alt=\".*src=(.*?)[^>]*?>";
    // 获取src路径的正则
    public static final String IMGSRC_REG = "http:\"?(.*?)(\"|>|\\s+)";
    // 获取img alt标签内容正则
    public static final String IMGALT_REG = "alt=\".*?\"";

    /**
     * 获取图片集html链接正则
     * <a target='_blank' href="http://www.meizitu.com/a/5399.html
     */
    public static final String HTML_REG = "<a\\starget='_blank'\\shref=\"http://www.meizitu.com/a/\\d{4}\\.html";

    /**
     * 获取图片集范围的html代码正则
     * http://www.meizitu.com/a/5399.html
     */
    public static final String IMAGE_LIST_REG = "http://www.meizitu.com/a/\\d{4}\\.html";

    /**
     * 获取下一页图片集html正则
     * <a href='xinggan_2_2.html'>下一页</a>
     */
    public static final String SITEURL_REG = "href='xinggan_\\d_\\d.html'>下一页</a>";
    /**
     * xinggan_2_2.html
     * 下一页图片集html正则
     */
    public static final String SITESRC_REG = "xinggan_\\d_\\d.html";

    /***
     * 获取ImageUrl地址
     *
     * @param HTML
     * @return List<Image>
     */
    public static List<Image> getImageUrl(String HTML) {
        String url = null;
        String alt = null;
        //现将图片所在的位置取出 初选
        Matcher matcher = Pattern.compile(PatternMatcher.IMGURL_REG).matcher(HTML);
        List<Image> imageList = new ArrayList<Image>();
        while (matcher.find()) {
            String UrlTemp = (matcher.group());
            //取图片的alt 作为保存图片的文件夹名称
            if (alt == null) {
                alt = PatternMatcher.getImageAlt(UrlTemp);
            }
            //再将图片url提取出来
            Matcher matcherSrc = Pattern.compile(PatternMatcher.IMGSRC_REG).matcher(UrlTemp);
            while (matcherSrc.find()) {
                url = matcherSrc.group();
                Image image = new Image(url.substring(url.lastIndexOf("/") + 1, url.length() - 1));
                image.setUrl(url.substring(0, url.length() - 1));
                image.setAlt(alt);
                image.setSavaPath(Main.BASICS_SAVA_PATH);
                imageList.add(image);
            }
        }
        return imageList;
    }

    /**
     * 获取 美女图片的alt标签信息
     * @param url
     * @return
     */
    public static String getImageAlt(String url) {
        Matcher matcher = Pattern.compile(PatternMatcher.IMGALT_REG).matcher(url);
        String alt = null;
        if (matcher.find()) {
            alt = matcher.group();
        }
        if (alt != null) {
            return alt.substring(alt.indexOf("\"") + 1, alt.lastIndexOf("，"));
        }
        return "美女";
    }

    /**
     * 获取美女图片url
     * @param html
     */
    public static void getPageHTML(String html, LinkSet linkSet) {
        String str = null;
        String url = null;
        Matcher matcher = Pattern.compile(PatternMatcher.HTML_REG).matcher(html);
        while (matcher.find()) {
            str = matcher.group();
            Matcher matcherSrc = Pattern.compile(PatternMatcher.IMAGE_LIST_REG).matcher(str);
            while (matcherSrc.find()) {
                url = matcherSrc.group();
                linkSet.addUnVisitURL(url.substring(0, url.length()));
            }
        }
    }

    /**
     * 获取 美女页面的下一页
     * @param html
     * @return
     */
    public static String getNextURL(String html) {
        Matcher matcher = Pattern.compile(PatternMatcher.SITEURL_REG).matcher(html);
        String url = null;
        if (matcher.find()) {
            url = matcher.group();
            Matcher matcher1 = Pattern.compile(PatternMatcher.SITESRC_REG).matcher(url);
            if (matcher1.find()) {
                return "http://www.meizitu.com/a/"+matcher1.group();
            }
        }
        return "";
    }
}
